package cn.kgc.vue.handle;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/8/10
 * @Description: 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    // 用户名不存在
    @ExceptionHandler(UnknownAccountException.class)
    public ResponseResult unknownAccountExceptionHandler(UnknownAccountException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.UNKNOWN_ACCOUNT);
        return responseResult;
    }

    // 密码错误   IncorrectCredentialsException
    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResponseResult incorrectCredentialsExceptionHandler(IncorrectCredentialsException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.INCORRECT_CREDENTIALS);
        return responseResult;
    }

    //账户被禁用  LockedAccountException
    @ExceptionHandler(LockedAccountException.class)
    public ResponseResult lockedAccountExceptionHandler(LockedAccountException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.LOCKED_ACCOUNT);
        return responseResult;
    }
    @ExceptionHandler(TokenExpiredException.class)
    public ResponseResult tokenExpiredExceptionHandler(TokenExpiredException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.TOKEN_EXPIRED);
        return responseResult;
    }

    // token异常
    @ExceptionHandler({SignatureVerificationException.class, JWTDecodeException.class})
    public ResponseResult tokenExceptionHandler(Exception e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.TOKEN_ERROR);
        return responseResult;
    }
    //token不存在
    @ExceptionHandler(AuthorizationException.class)
    public ResponseResult authorizationexception(AuthorizationException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.AUTHORIZATION_ERROR);
        return responseResult;
    }

    @ExceptionHandler(AuthenticationException.class)
    public ResponseResult authenticationexception(AuthenticationException e){
        e.printStackTrace();
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.APPROVE_ACCOUNT);
        return responseResult;
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseResult runtimeexception(RuntimeException e){
        ResponseResult responseResult = new ResponseResult();
        responseResult.fail(ResponseStatus.STUDENTNO_ACCOUNT);
        return responseResult;
    }
}
